package com.example.armmvvm.ui.customdraw.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.example.arm.ext.dp
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.sin

/**
 * 饼状图
 *
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.example.armmvvm.ui.main
 * @createTime 创建时间 ：2021/9/13
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class PieTestView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.BLUE
        style = Paint.Style.FILL
        strokeWidth = 3.dp
    }

    private val angleList = arrayOf(
        60f, 90f, 150f, 60f
    )

    private val colorList = arrayOf(
        Color.parseColor("#987100"),
        Color.parseColor("#981f00"),
        Color.parseColor("#567100"),
        Color.parseColor("#acac00"),
    )

    // 仪表盘的宽度
    private val diameter = 200.dp

    private val radius = diameter / 2

    private var startX = 0f

    private var endX = 0f

    private var endY = 0f

    private var startY = 0f

    private var selectIndex = -1

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        startX = width.toFloat() / 2 - radius
        endX = width.toFloat() / 2 + radius
        endY = height.toFloat() / 2 + radius
        startY = height.toFloat() / 2 - radius
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        var startAngle = 0f
        for ((index, value) in angleList.withIndex()) {
            paint.color = colorList[index]
            if (index == selectIndex) {
                canvas.save()
                canvas.translate(
                    (10.dp * cos(Math.toRadians((startAngle + value / 2).toDouble()))).toFloat(),
                    (10.dp * sin(Math.toRadians((startAngle + value / 2).toDouble()))).toFloat()
                )
            }
            canvas.drawArc(
                startX,
                startY,
                endX,
                endY,
                startAngle,
                value,
                true,
                paint
            )
            startAngle += value
            if (index == selectIndex) {
                canvas.restore()
            }
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_UP) {
            var angle = Math.toDegrees(atan2(event.y - height / 2, event.x - width / 2).toDouble())
            if (angle < 0) {
                angle += 360.0
            }
            var startAngle = 0f
            for ((index, value) in angleList.withIndex()) {
                if (angle <= value + startAngle) {
                    selectIndex = if (selectIndex == index) {
                        -1
                    } else {
                        index
                    }
                    invalidate()
                    return true
                }
                startAngle += value
            }
        } else {
            return true
        }
        return super.onTouchEvent(event)
    }

}